package com.foreknow.demo6;

import java.util.Scanner;

/**
 * 思路：
 * 1. 根据需求思考需要用定义哪些方法
 *      welcome()--------------输出菜单信息
 *      login()----------------登录方法
 *      checkMoney()-----------查询余额
 *      setMoney()-------------存款
 *      getMoney()-------------取款
 *      changePwd()------------修改密码
 * 2. 用户需要根据条件选择菜单  switch语法
 *
 * 3 . 定义变量  user   pwd   money
 */
public class BankSystem {
    public static void welcome() {
        System.out.println("*******************");
        System.out.println("Bank System");
        System.out.println("*******************");
    }

    /**
     * 登录
     * @param user 用户名
     * @param pwd 密码
     * @return boolean
     */
    public static boolean login(String user,String pwd) {
        Scanner input = new Scanner(System.in);
        for(int i = 3;i>0;i--) {
            System.out.println("请输入用户名: ");
            String newUser = input.next();
            System.out.println("请输入密码: ");
            String newPwd = input.next();
            if(user.equals(newUser) && pwd.equals(newPwd)) {
                //登录成功
                System.out.println("登录成功");
                return true;
            }else {
                if(i - 1 == 0) {
                    System.out.println("密码3次错误,吞卡");
                    System.exit(0);
                }
                System.out.println("用户名或密码错误，今日还有"+(i - 1)+"次机会");
            }
        }

        return false;
    }

    public static void checkMoney(double money) {
        System.out.println("您的余额: " + money);
    }

    public static double setMoney(double money) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您要存款的金额");
        double addMoney = input.nextInt();
        if(addMoney > 10000) {
            System.out.println("单次存款金额超过10000");
            return 0;
        }else if(addMoney < 0) {
            System.out.println("存款金额错误！！！");
            return 0;
        }else if(addMoney % 100 != 0) {
            System.out.println("请输入100的整数倍存款");
            return 0;
        }else {
            System.out.println("存款成功，当前的余额: " + (money + addMoney));
            return addMoney;
        }

    }

    public static double getMoney(double money) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入您要取款的金额");
        double subMoney = input.nextInt();
        if(subMoney > money) {
            System.out.println("余额不足");
            return 0;
        }else if(subMoney > 3000) {
            System.out.println("单次的取款金额超过3000");
            return 0;
        }else if(subMoney < 0) {
            System.out.println("取款金额错误！！！");
            return 0;
        } else if(subMoney % 100 != 0) {
            System.out.println("请取出100的整数倍存款");
            return 0;
        }else {
            System.out.println("存款成功，当前的余额: " + (money - subMoney));
            return subMoney;
        }
    }

    public static String changePwd(String pwd) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入旧密码: ");
        String oldPwd = input.next();
        if(oldPwd.equals(pwd)) {
            System.out.println("请输入新密码: ");
            String newPwd = input.next();
            System.out.println("请输入确认密码: ");
            String confirmPwd = input.next();
            if(newPwd.equals(confirmPwd)) {
                System.out.println("修改密码成功");
                return newPwd;
            }else {
                System.out.println("两次输入的密码不一致，请重新输入");
                return pwd;
            }
        }else {
            System.out.println("旧密码输入错误");
        }
        return pwd;
    }

    /**
     * 是否继续(y/n)?
     */
    public static void decide() {
        Scanner input = new Scanner(System.in);
        System.out.println("是否继续(y/n)");
        String dec = input.next();
        if(dec.equals("y")) {
            return;
        }else if(dec.equals("n")) {
            System.out.println("退出系统！！！");
            System.exit(0);
        }else {
            while (true) {
                System.out.println("输入错误，请重新输入，是否继续(y/n)");
                String temp = input.next();
                if(temp.equals("y")) {
                    return;
                }else if(temp.equals("n")) {
                    System.out.println("退出系统！！！");
                    System.exit(0);
                }
            }
        }
    }

    public static void main(String[] args) {
        String user = "admin";
        String pwd  = "123";
        double money = 10000.0;
        Scanner input = new Scanner(System.in);
        BankSystem.welcome();
        //调用登录方法
        boolean isRight = BankSystem.login(user,pwd);
        while (isRight) {
            System.out.println("1. 查询余额    2. 存款     3. 取款    4. 修改密码    5. 系统退出"   );
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    //1. 查询余额
                    BankSystem.checkMoney(money);
                    BankSystem.decide();
                    break;
                case 2:
                    //2. 存款
                    //double addMoney = BankSystem.setMoney(money);
                    money += BankSystem.setMoney(money);
                    BankSystem.decide();
                    break;
                case 3:
                    //3. 取款
                    money -= BankSystem.getMoney(money);
                    BankSystem.decide();
                    break;
                case 4:
                    //4. 修改密码
                    String newPwd = BankSystem.changePwd(pwd);
                    System.out.println(newPwd);
                    BankSystem.decide();
                    break;
                case 5:
                    //5. 系统退出
                    System.exit(0);
                    //System.out.println("系统退出！！！");
                    break;
            }
        }

    }
}
